<?php
class SaveLink extends Action {
	/*
	 * (non-PHPdoc) @see Action::execute()
	 */
	public function execute($params) {
		check_login ( ADMIN_LOGIN_PAGE );
		$link = rqsts ( array ('link_id' => 0,'title','name','note','display' => 1,'target' => '_blank','url','type','icon','sort' => 9999 ), true );
		
		$linkModel = new WebLinkModel ();
		
		if (empty ( $link ['link_id'] )) { // 新增
			unset ( $link ['link_id'] );
		}
		if (empty ( $link ['type'] )) {
			$link ['type'] = "未分类";
		}
		// 验证用户数据的合法性
		$valid = $this->validate ( 'link', $link );
		if ($valid === true) { // 用户数据合法
			$rst = $linkModel->save ( $link );
			if (! empty ( $rst )) {
				if (! rqset ( 'goon' )) {
					sess_add ( 'page_tip_info', '<strong>恭喜,</strong>友情链接已经保存完成.' );
					$link ['link_id'] = $rst ['link_id'];
					sess_add ( 'link_form_data', $link );
					Response::redirect ( './?Ctlr=EditLink&suc=1&lid=' . $rst ['link_id'] );
				} else {
					Response::redirect ( './?Ctlr=AddLink' );
				}
			} else {
				sess_add ( 'link_form_data', $link );
				sess_add ( 'page_tip_info', '<strong>出错啦!</strong>' . $linkModel->last_error_msg () );
				Response::redirect ( $this->referer );
			}
		}
		sess_add ( 'link_form_error', $valid );
		sess_add ( 'link_form_data', $link );
		Response::redirect ( $this->referer );
	}
}